﻿package view.popup
{
    import com.core.*;
    import com.sh.game.global.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import plugin.bagN.*;
    import plugin.roleN.model.*;
    import view.controller.*;
    import view.light.*;
    import view.mornExtends.*;

    public class NewUpDialog extends View implements IDialogUI
    {
        public var closeBtn:Button;
        public var icon_skill:FlashItem;
        public var nameLabel:Label;
        public var sureBtn:Button;
        public var armBox:Box;
        public var skillBox:Box;
        public var item_name:Label;
        public var sm_nr:Label;
        protected var uiXML:XML;
        private var _leveltext:TextField;
        private var item:Loader;
        private var upbmp:Bitmap;
        private var _type:int = 0;
        private var _data:Object = null;
        private var dataArr:Array;
        private var oldicon:String = "";
        private var powsp:Sprite;
        private var _timeID:uint = 0;
        private var calltime:int = 5;
        private var cfgitem:Object;

        public function NewUpDialog()
        {
            this.uiXML = <View>r
n	t	t	t  <Image url=""png.common.cs2_tsbg"" x=""1"" y=""1"" width=""326"" height=""204"" sizeGrid=""10,50,10,50""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_close"" x=""297"" y=""5"" var=""closeBtn"" labelColors=""0xf3c76d,0xffe0a0,0x7e6b43""/>r
n	t	t	t  <Image url=""jpg.common.neibg"" x=""6"" y=""35"" mouseEnabled=""false""/>r
n	t	t	t  <FlashItem x=""30"" y=""67"" var=""icon_skill"" width=""70"" height=""70"">r
n	t	t	t	t<Image url=""png.common.img_2"" width=""70"" height=""70""/>r
n	t	t	t	t<Image url=""png.common.noramlicon"" x=""5"" y=""5"" width=""61"" height=""61"" name=""icon""/>r
n	t	t	t  </FlashItem>r
n	t	t	t  <Label text=""刺杀剑法"" x=""79"" y=""8"" color=""0xffe4c7"" stroke=""0x0"" width=""168"" height=""21"" align=""center"" bold=""true"" size=""14"" font=""SimSun"" var=""nameLabel"" mouseEnabled=""false""/>r
n	t	t	t  <Button label=""使用物品"" skin=""png.common.btn_cs2_bag"" x=""126"" y=""155"" labelColors=""0xb29a6c,0xb29a6c,0xfff7af"" labelFont=""SimSun"" labelStroke=""1"" var=""sureBtn"" labelBold=""false"" width=""77"" height=""26""/>r
n	t	t	t  <Box x=""109"" y=""70"" var=""armBox"">r
n	t	t	t	t<Label text=""您获得了一件更好的装备："" color=""0x43b436"" stroke=""0x0"" mouseEnabled=""false"" x=""10""/>r
n	t	t	t	t<UrlImage url=""skill/zhandoul.png"" y=""21""/>r
n	t	t	t  </Box>r
n	t	t	t  <Box x=""121"" y=""70"" var=""skillBox"">r
n	t	t	t	t<Label text=""获得：《刺杀剑法》技能秘籍"" y=""41"" color=""0x43b436"" stroke=""0x0"" mouseEnabled=""false"" width=""182"" height=""18"" var=""item_name"" isHtml=""true""/>r
n	t	t	t	t<Label text=""点击学习“刺杀剑法”威力大增！"" color=""0x4ba0de"" stroke=""0x0"" var=""sm_nr"" letterSpacing=""2"" mouseEnabled=""false""/>r
n	t	t	t  </Box>r
n	t	t	t</View>")("<View>
			  <Image url="png.common.cs2_tsbg" x="1" y="1" width="326" height="204" sizeGrid="10,50,10,50"/>
			  <Button skin="png.common.btn_cs2_close" x="297" y="5" var="closeBtn" labelColors="0xf3c76d,0xffe0a0,0x7e6b43"/>
			  <Image url="jpg.common.neibg" x="6" y="35" mouseEnabled="false"/>
			  <FlashItem x="30" y="67" var="icon_skill" width="70" height="70">
				<Image url="png.common.img_2" width="70" height="70"/>
				<Image url="png.common.noramlicon" x="5" y="5" width="61" height="61" name="icon"/>
			  </FlashItem>
			  <Label text="刺杀剑法" x="79" y="8" color="0xffe4c7" stroke="0x0" width="168" height="21" align="center" bold="true" size="14" font="SimSun" var="nameLabel" mouseEnabled="false"/>
			  <Button label="使用物品" skin="png.common.btn_cs2_bag" x="126" y="155" labelColors="0xb29a6c,0xb29a6c,0xfff7af" labelFont="SimSun" labelStroke="1" var="sureBtn" labelBold="false" width="77" height="26"/>
			  <Box x="109" y="70" var="armBox">
				<Label text="您获得了一件更好的装备：\" color="0x43b436" stroke="0x0" mouseEnabled="false" x="10"/>
				<UrlImage url="skill/zhandoul.png" y="21"/>
			  </Box>
			  <Box x="121" y="70" var="skillBox">
				<Label text="获得：《刺杀剑法》技能秘籍\" y="41" color="0x43b436" stroke="0x0" mouseEnabled="false" width="182" height="18" var="item_name" isHtml="true"/>
				<Label text="点击学习“刺杀剑法”威力大增！" color="0x4ba0de" stroke="0x0" var="sm_nr" letterSpacing="2" mouseEnabled="false"/>
			  </Box>
			</View>;
            this._leveltext = new TextField();
            this.dataArr = [];
            this.cfgitem = ConfigDictionary.data.item;
            return;
        }// end function

        override protected function createChildren() : void
        {
            createView(this.uiXML);
            this.init();
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            LightFactory.showlight(this.sureBtn);
            TweenVisibleManager.instance.register(this, TweenVisibleManager.Up, TweenVisibleManager.Right);
            return;
        }// end function

        public function init() : void
        {
            this.visible = false;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width - 50;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height - 100;
            this.powsp = new Sprite();
            this.powsp.x = 90;
            this.powsp.y = 25;
            this.armBox.addChild(this.powsp);
            this.sureBtn.label = "确认";
            this.sureBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            this.sureBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onUse);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onUse);
            this.closeBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function onUse(event:MouseEvent = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:Object = null;
            var _loc_8:Object = null;
            this.clearTime();
            if (this._data == null)
            {
                this.close();
                return;
            }
            if (this._data[4] && this._data[4].cls == 23)
            {
                ModelProxy.change("main", "playsound", SoundType.DZ_5);
                PluginManager.instance.sendToServer("bag", CMDBag.UseItem, int(this._data.index));
                ModelProxy.change("data", "changeid", this._data[4].itemid);
            }
            else if (this._type == 1)
            {
                _loc_2 = this.getPowValue(this._data[4]);
                _loc_3 = ModelProxy.getValue("data", "arms");
                _loc_4 = this._data[4].cls;
                if (_loc_4 == 3 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[10]))
                    {
                        _loc_4 = 10;
                    }
                }
                else if (_loc_4 == 2 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[9]))
                    {
                        _loc_4 = 9;
                    }
                }
                _loc_5 = _loc_3[_loc_4];
                _loc_6 = 0;
                if (_loc_5 != null)
                {
                    _loc_6 = this.getPowValue(_loc_5);
                }
                if (_loc_6 >= _loc_2)
                {
                    this.close();
                    return;
                }
                _loc_7 = new Object();
                _loc_8 = this._data[4];
                _loc_7.pos = 0;
                _loc_4 = _loc_8.cls;
                if (_loc_4 == 3 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[10]))
                    {
                        _loc_7.pos = 10;
                    }
                }
                else if (_loc_4 == 2 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[9]))
                    {
                        _loc_7.pos = 9;
                    }
                }
                _loc_7.armid = this._data[0];
                _loc_7.index = this._data.index;
                PluginManager.instance.sendToServer("role", CMDRole.PutOn, _loc_7);
            }
            this.close();
            return;
        }// end function

        private function getpower(param1:Sprite, param2:int) : void
        {
            var _loc_8:BitmapData = null;
            var _loc_9:Bitmap = null;
            var _loc_3:* = param1.numChildren;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                param1.removeChildAt(0);
                _loc_4++;
            }
            var _loc_5:* = String(Math.abs(param2));
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5.length)
            {
                
                _loc_8 = App.asset.getBitmapData("png.common.pow2_" + _loc_5.substr(_loc_6, 1));
                _loc_9 = new Bitmap(_loc_8);
                _loc_9.x = 3 + _loc_6 * 21;
                param1.addChild(_loc_9);
                _loc_6++;
            }
            var _loc_7:* = App.asset.getBitmapData("png.common.unupp");
            this.upbmp = new Bitmap(_loc_7);
            this.upbmp.x = _loc_5.length * 21 + 12;
            param1.addChild(this.upbmp);
            return;
        }// end function

        private function getPowValue(param1:Object) : int
        {
            var _loc_17:Array = null;
            var _loc_18:String = null;
            var _loc_19:Array = null;
            var _loc_20:int = 0;
            var _loc_21:Object = null;
            var _loc_22:Object = null;
            if (param1 == null || !param1.hasOwnProperty("itemid"))
            {
                return 0;
            }
            return param1.nbvalue;
            if (_loc_16 != null)
            {
                for each (_loc_20 in _loc_16)
                {
                    
                    if (_loc_20 > 1)
                    {
                        _loc_21 = ConfigDictionary.data.item;
                        _loc_22 = _loc_21[_loc_20];
                        if (_loc_22 != null)
                        {
                            if (_loc_3 > 0)
                            {
                                var _loc_13:* = _loc_13 + int(_loc_22.phyAttMax);
                                var _loc_14:* = _loc_14 + int(_loc_22.phyAtt);
                            }
                            else if (_loc_5 > 0)
                            {
                                _loc_13 = _loc_13 + int(_loc_22.magicAttMax);
                                _loc_14 = _loc_14 + int(_loc_22.magicAtt);
                            }
                            else if (_loc_7 > 0)
                            {
                                _loc_13 = _loc_13 + int(_loc_22.taoAttMax);
                                _loc_14 = _loc_14 + int(_loc_22.taoAtt);
                            }
                            if (_loc_22.hp > 0)
                            {
                                var _loc_11:* = _loc_11 + int(_loc_22.hp);
                            }
                            if (_loc_22.mp > 0)
                            {
                                var _loc_12:* = _loc_12 + int(_loc_22.mp);
                            }
                            if (_loc_22.phyDefMax > 0)
                            {
                                var _loc_9:* = _loc_9 + int(_loc_22.phyDefMax);
                            }
                            if (_loc_22.magicDefMax > 0)
                            {
                                var _loc_10:* = _loc_10 + int(_loc_22.magicDefMax);
                            }
                        }
                    }
                }
            }
            var _loc_2:* = int(((_loc_13 + _loc_14) / 2 + (_loc_9 + _loc_10) * 0.3 + _loc_11 / 10 + _loc_12 / 30) * 2);
            return _loc_2;
        }// end function

        public function show(param1:Object) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            if (this._data && this._data[0] == param1[0])
            {
                return;
            }
            TweenVisibleManager.instance.changevisible(this, true);
            this._type = param1[1];
            this._data = param1;
            param1 = param1[4];
            this.icon_skill.data = this._data[4];
            this.icon_skill.changeSuperLvXY(15, 4);
            this.nameLabel.text = param1.name;
            if (param1.cls == 23)
            {
                this.sureBtn.label = "学习技能";
                this.item_name.text = "获得：《" + param1.name + "》技能秘籍";
                this.sm_nr.color = 4956382;
                this.sm_nr.text = "点击学习" + param1.name + "威力大增！";
                this.skillBox.visible = true;
                this.armBox.visible = false;
            }
            else
            {
                this.powsp.visible = true;
                this.sureBtn.label = "使用(5)";
                _loc_2 = param1.nbvalue;
                _loc_3 = ModelProxy.getValue("data", "arms");
                _loc_4 = param1.cls;
                if (_loc_4 == 3 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[10]))
                    {
                        _loc_4 = 10;
                    }
                }
                else if (_loc_4 == 2 && _loc_3[_loc_4] != null)
                {
                    if (this.getPowValue(_loc_3[_loc_4]) > this.getPowValue(_loc_3[9]))
                    {
                        _loc_4 = 9;
                    }
                }
                _loc_5 = _loc_3[_loc_4];
                _loc_6 = 0;
                if (_loc_5 != null)
                {
                    _loc_6 = _loc_5.nbvalue;
                }
                _loc_7 = _loc_2 - _loc_6;
                this.getpower(this.powsp, _loc_7);
                this.skillBox.visible = false;
                this.armBox.visible = true;
            }
            this.clearTime();
            this._timeID = setInterval(this.readtime, 1000);
            return;
        }// end function

        private function clearTime() : void
        {
            if (this._timeID > 0)
            {
                clearInterval(this._timeID);
                this._timeID = 0;
                this.calltime = 5;
            }
            return;
        }// end function

        private function readtime() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.calltime - 1;
            _loc_1.calltime = _loc_2;
            this.sureBtn.label = "使用(" + this.calltime + ")";
            if (this.calltime <= 0)
            {
                this.onUse();
            }
            return;
        }// end function

        private function onClose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            TweenVisibleManager.instance.changevisible(this, false);
            this._data = null;
            PowerCheckControl.instance.showPowerUp();
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width - 50;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height - 100;
            TweenVisibleManager.instance.register(this, TweenVisibleManager.Up, TweenVisibleManager.Right);
            return;
        }// end function

    }
}
